package com.ts.api.http.org.biz;

import com.alibaba.fastjson.JSONObject;
import com.gitee.apanlh.util.algorithm.encrypt.symmetric.AES;
import com.gitee.apanlh.util.base.ChooseEq;
import com.gitee.apanlh.util.base.Eq;
import com.gitee.apanlh.util.base.MapUtils;
import com.gitee.apanlh.util.dataformat.JsonUtils;
import com.gitee.apanlh.util.valid.ValidParam;
import com.ts.api.http.org.entity.apply.OrgApplyGuoYun;
import com.ts.api.http.org.entity.dataexchange.OrgDataExchangeGuoYun;
import com.ts.api.module.api.convert.core.annotation.ApiPlan;
import com.ts.api.module.api.convert.core.annotation.ApiPlanConvertType;
import com.ts.api.module.api.convert.core.entity.ApiApplyParameter;
import com.ts.api.module.api.convert.core.entity.ApiDataExchangeParameter;
import com.ts.api.module.api.convert.core.utils.ApiFieldConverter;
import com.ts.api.module.api.convert.org.handler.ApiOrgPhoneMd5ConvertHandler;
import com.ts.api.module.api.convert.org.model.RespOrgPhoneMd5Apply;
import com.ts.api.module.api.convert.org.model.RespOrgPhoneMd5DataExchange;

import java.util.Map;

/**
 *  山西国云
 *
 *  @author WJB
 */
@ApiPlan(productId = "50", type = ApiPlanConvertType.ORG_PHONE_MD5)
public class ApiOrgPhoneMd5GuoYun implements ApiOrgPhoneMd5ConvertHandler {
    private static final String KEY = "shanxiguoyun1234";
    @Override
    public Object dataExchange(ApiDataExchangeParameter apiDataExchangeParameter) {
        OrgDataExchangeGuoYun data = new OrgDataExchangeGuoYun();
        data.setMd5_phone(apiDataExchangeParameter.getPhoneMd5());
        data.setChannel_code(19);
        return data;
    }

    @Override
    public RespOrgPhoneMd5DataExchange respDataExchange(String respJson) {
        JSONObject jsonObject = JsonUtils.get(respJson);
        if (jsonObject == null) {
            return RespOrgPhoneMd5DataExchange.ofFail();
        }

        boolean code = Eq.object(jsonObject.getInteger("code"), 0);

        if (!code) {
            return RespOrgPhoneMd5DataExchange.ofFail();
        }
        return RespOrgPhoneMd5DataExchange.ofSuc();
    }

    @Override
    public Object apply(ApiApplyParameter apiApplyParameter) {
        OrgApplyGuoYun data = new OrgApplyGuoYun();
        data.setChannel_code(19);
        data.setName(apiApplyParameter.getUserName());
        data.setPhone(apiApplyParameter.getPhone());
        Integer sex = ChooseEq.create()
                .when(apiApplyParameter.getSex(), "0", 1)
                .when(apiApplyParameter.getSex(), "1", 2)
                .end(0);
        data.setSex(sex);

        //  拓展判断信息
        ApiFieldConverter apiFieldConverter = ApiFieldConverter.create(apiApplyParameter);
        //  年龄
        data.setAge(apiFieldConverter.getAgeInt());
        //  贷款额度
        if (ValidParam.isNotEmpty(apiApplyParameter.getLoanAmount())) {
            Integer money = ChooseEq.create()
                    .when(apiApplyParameter.getLoanAmount(), "1", 3)
                    .when(apiApplyParameter.getLoanAmount(), "2", 5)
                    .when(apiApplyParameter.getLoanAmount(), "3", 10)
                    .when(apiApplyParameter.getLoanAmount(), "4", 20)
                    .end();
            data.setQuota(money);
        }
        //  房
        if (ValidParam.isNotEmpty(apiApplyParameter.getRealEstateInfo())) {
            Integer text = ChooseEq.create()
                    .when(apiApplyParameter.getRealEstateInfo(), "1", 0)
                    .end(1);
            data.setHouse(text);
        }
        //  车
        data.setCar(apiFieldConverter.hasCarInfoToNumber());
        //  社保
        data.setSocial_security(apiFieldConverter.hasSocialSecurityToNumber());
        //  信用卡
        data.setCredit_card(apiFieldConverter.hasCreditLimitToNumber());
        //  营业执照
        data.setBusiness_license(apiFieldConverter.hasBizLicenseYearToNumber());
        //  公积金
        data.setAccumulation_fund(apiFieldConverter.hasProvidentFundToNumber());
        //  保单
        data.setInsurance_policy(apiFieldConverter.hasInsurancePolicyToNumber());
        //  代发
        if (ValidParam.isNotEmpty(apiApplyParameter.getSalaryPayMethod())) {
            Integer text = ChooseEq.create()
                    .when(apiApplyParameter.getSalaryPayMethod(), "1", 1)
                    .end(0);
            data.setHava_issued(text);
        }
        //  身份证
        data.setId_card(apiApplyParameter.getIdcard());
        //  职业
        if (ValidParam.isNotEmpty(apiApplyParameter.getCareer())) {
            Integer text = ChooseEq.create()
                    .when(apiApplyParameter.getCareer(), "1", 1)
                    .when(apiApplyParameter.getCareer(), "2", 0)
                    .when(apiApplyParameter.getCareer(), "3", 2)
                    .when(apiApplyParameter.getCareer(), "4", 1)
                    .when(apiApplyParameter.getCareer(), "5", 1)
                    .end();
            data.setProfessional_identity(text);
        }
        AES ecb = AES.createEcb(KEY.getBytes());
        String encrypt = ecb.encryptToBase64Str(JsonUtils.toJson(data));
        Map<Object, Object> map = MapUtils.newHashMap(newMap -> {
            newMap.put("channel_code", 19);
            newMap.put("data", encrypt);
        });
        return map;
    }

    @Override
    public RespOrgPhoneMd5Apply respApply(String respJson) {
        JSONObject jsonObject = JsonUtils.get(respJson);
        if (jsonObject == null) {
            return RespOrgPhoneMd5Apply.ofFail();
        }
        if (!Eq.object(jsonObject.getInteger("code"), 0)) {
            return RespOrgPhoneMd5Apply.ofFail();
        }
        return RespOrgPhoneMd5Apply.ofSuc();
    }
}
